<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>函数重载</title>
    <script>
        function createOverload(){
            const fnMap = new Map();
            function overload(...args){
                const key = args.map(it=>typeof it).join(',');
                const fn = fnMap.get(key);
                if(!fn){
                    throw new TypeError('没有找到对应的实现');   
                }

                return fn.apply(this,args);
            }
            overload.addImpl=function(...args){
                const fn = args.pop();
                if(typeof fn !== 'function'){
                    throw new TypeError('最后一个参数必须是函数');
                }
                const key = args.join(',');
                fnMap.set(key,fn);
            };
            return overload;
        }

        const getUsers = createOverload();
        getUsers.addImpl(()=>{
            console.log('查询所有用户');
        })

        const searchPage = (page,size=10)=>{
            console.log('按照页码和数量查询用户');
        }
        getUsers.addImpl('number',searchPage);
        getUsers.addImpl('number','number',searchPage);

        getUsers.addImpl('string',(name)=>{
            console.log(`按照${name}查询用户`);
        });

        getUsers.addImpl('string','string',(name,sex)=>{
            console.log(`按照${name}:${sex}查询用户`);
        });

        getUsers(1)
        getUsers(1,1)
        getUsers('小红')
        getUsers('小红','666')
    </script>
</head>
<body>
    
</body>
</html>